/**
 * <p>
 * dataModel数据寄存类
 * </p>
 * @class
 */
zxt.data.Composite = function(){	
	this._sources = [];
	this._async = true;	
};

/**
 * <p>
 * DataModel对象
 * </p>
 * @function
 * @param {object} s DataModel对象
 */
zxt.data.Composite.prototype.append = function(s){
	this._sources.push(s);
};

/**
 * <p>
 * 将数据内容加载到数组中
 * </p>
 * @function
 * @param {Object} dest 目标数组
 */
zxt.data.Composite.prototype.loadTo = function(dest){
	for( var i=0; i<this._sources.length; i++ ){
		var s = this._sources[i];
		
		if ( zxt.util.isValue(s["instanceOf"]) && s.instanceOf(zxt.data.TableModel) ){
			var items = s.get(0);
			for( var j=0; j<items.length; j++ ){ dest.push( items[j] ); };	
		} else {
			dest.push(s);
		};
	};	
};	

/**
 * <p>
 * 将数据内容加载到下拉列表中
 * </p>
 * @function
 * @param {Object} sel 下拉列表对象
 */
zxt.data.Composite.prototype.loadToSelect = function(sel){
	var stks = [];
	
	var loadT = function(){		
			var opts = [];	
			for( var i=0; i<stks.length; i++ ){ 
				var stk = stks[i];
				if ( stk == null ){ return false; }
				if ( zxt.util.isArray(stk) ){
					for( var j=0; j<stk.length; j++ ){
						opts.push(stk[j]);
					};	 
				} else {
					opts.push(stk);
				};		
			};
			for( var i=0; i<opts.length; i++ ){ sel.appendOption(opts[i]); };	
			return true;
	};
	
	var tt = function(idx) {    
		this.onSuccess = function(dw, items) {			
			stks[idx]=items;
			if( loadT() )	sel.render();
		};
		
		this.onFailure = function(xmlR, status, e){};
	};
		
	for( var i=0; i<this._sources.length; i++ ){
		var s = this._sources[i];		
		if ( zxt.util.isValue(s["instanceOf"]) && s.instanceOf(zxt.data.TableModel) ){			
			if ( s.instanceOf(zxt.data.TunnelTableModel) && s.async() ){
				stks[i] = null;
				var cb = new tt(i);
				s.callback(cb);
				s.get(0);
			}	else {
				var items = s.get(0);
				stks[i] = items;
			}			
		} else {
			stks[i] = s;
		};	
	};
	
	loadT();	
};	

/**
 * <p>
 * 将dom节点转化为选项
 * </p>
 * @function
 * @private
 * @param {Dom} elem 选项的Dom节点
 * @return {zxt.data.Composite}对象
 */
zxt.data.parseOptions = function(elem) {
	var j = $(elem);
	
	var _instance = new zxt.data.Composite();
	
	var _options = j.attr("zxt.options");	
	if ( zxt.util.isAvailable(_options) ){
		var options = zxt.util.cutStr(_options);	
		for(var i = 0;i<options.length;i++){
			_instance.append(options[i]);
		};
	};
	
	$(">div", j ).each( function( idx, dom )	{		
		var _option = $(dom).attr("zxt.option");
		var _service = $(dom).attr("zxt.service");
		
		if ( zxt.util.isValue(_option) ){
			var _label = $(dom).attr("zxt.label");			
			_instance.append( {value:_option, label:_label} );		
		} else if ( zxt.util.isValue(_service) ){
			var _method = $(dom).attr("zxt.method") || "query";
			var _param = $(dom).attr("zxt.param");
			var _async = $(dom).attr("zxt.async");
			var async = (_async == "true")? true: false;
			
			var model = new zxt.data.TunnelTableModel(_service);
			model.method(_method);
			model.async(async);
			param = eval("(" + _param + ")");
			model.setParam(param);
			
			_instance.append(model);			
		};	
	});	
	
	return _instance;
};	



